package com.summerxia.dateselector.widget;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.RelativeLayout;

import com.summerxia.dateselector.R;
import com.summerxia.dateselector.animation.Effectstype;
import com.summerxia.dateselector.utils.DateUtils;

import java.util.List;


public class MsgPeopleSelectorDialogBuilder extends NiftyDialogBuilder implements
        View.OnClickListener {

    private Context context;
    private RelativeLayout rlCustomLayout;
    private NewsCompontentWheel newsCompontentWheel;
    private OnSaveLocationLister saveLocationLister;
    private static MsgPeopleSelectorDialogBuilder instance;
    private static  int mOrientation=1;

    public interface OnSaveLocationLister {
        abstract void onSaveLocation(String location, String provinceId, String cityId);
    }

    public MsgPeopleSelectorDialogBuilder(Context context, int theme) {
        super(context, theme);
        this.context = context;
        initDialog();
    }

    public MsgPeopleSelectorDialogBuilder(Context context) {
        super(context);
        this.context = context;
        initDialog();
    }

    public static MsgPeopleSelectorDialogBuilder getInstance(Context context){

        int ort=context.getResources().getConfiguration().orientation;
        if (mOrientation!=ort){
            mOrientation=ort;
            instance=null;
        }

        if (instance == null||((Activity) context).isFinishing()) {
            synchronized (MsgPeopleSelectorDialogBuilder.class) {
                if (instance == null) {
                    instance = new MsgPeopleSelectorDialogBuilder(context,R.style.dialog_untran);
                }
            }
        }
        return instance;

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setBackgroundDrawableResource(R.drawable.edit_dialog_coner);
    }

    private void initDialog() {
        rlCustomLayout = (RelativeLayout) LayoutInflater.from(context).inflate(
                R.layout.compontent_selector_dialog_layout, null);
        newsCompontentWheel = (NewsCompontentWheel) rlCustomLayout
                .findViewById(R.id.aw_location_selector_wheel);
        setDialogProperties();
    }

    private void setDialogProperties() {
        int width = DateUtils.getScreenWidth(context);
        this.withDialogWindows(width, LayoutParams.WRAP_CONTENT)
                .withTitleColor("#000000").withTitle("选择消息人")
                .setDialogClick(this).withEffect(Effectstype.Slit)
                .withIcon(R.drawable.choose)
                .withDuration(200).setPreviousLayoutClick(this)
                .withNextImage(R.drawable.choose_sure)
                .withMessageMiss(View.GONE).withNextTextColor("#3598da")
                .setNextLayoutClick(this)
                .setCustomView(rlCustomLayout, context);
    }

    public void setNewsCompontents(List<String> newsCompontents) {
        newsCompontentWheel.setNewsCompontents(newsCompontents);
    }

    public void setClassifys(List<String> classifys,int num) {
        newsCompontentWheel.setClassifys(classifys,num);
    }


    public void setOnlyClassify(){
        newsCompontentWheel.setOnlyClassify();
    }

    public NewsCompontentWheel getNewsCompontentWheel() {
        return newsCompontentWheel;
    }

    @Override
    public void onClick(View v) {
        int id = v.getId() ;
        if(id == R.id.fl_dialog_title_previous ) {
            dismiss();
        }else if(id == R.id.fl_dialog_title_next){
            if (null != saveLocationLister) {
                saveLocationLister.onSaveLocation(newsCompontentWheel.getNewsName(), newsCompontentWheel.getCompontentId(), newsCompontentWheel.getClassifyId());
            }
            dismiss();
        }
    }
    /**
     * 设置点击保存的监听
     * @param saveLocationLister
     */
    public void setOnSaveLocationLister(OnSaveLocationLister saveLocationLister) {
        this.saveLocationLister = saveLocationLister;
    }
    @Override
    public void dismiss() {
        super.dismiss();
        instance = null;
    }


}
